package com.dome.gateway.rule;

import com.dome.gateway.config.RulePropertiesConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @description:
 * @author: hz
 * @time: 2021/5/3 15:42
 */
public class RibbonLoadbalancerRuleConfiguration implements InitializingBean {

    @Autowired
    RulePropertiesConfig rulePropertiesConfig;


    private static final String RIBBON = "ribbon.";
    private static final Logger logger = LoggerFactory.getLogger(RibbonLoadbalancerRuleConfiguration.class);

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, String> ribbonMap = rulePropertiesConfig.getRibbon();
        logger.info("init ribbonMap finished");

        if(ribbonMap.isEmpty()) {
            throw new RuntimeException("ribbonMap must be not empty");
        }
        //刷新内存中robbin.的配置
        ribbonMap.forEach((k,v)->{
            System.setProperty(RIBBON+k,v);
        });
    }
}